package cn.chia.back.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.back.dao.IAddressDAO;
import cn.chia.pojo.Address;
import cn.chia.pojo.User;

@Component
public class AddressDAOImpl extends HibernateDaoSupport implements IAddressDAO {

	@Autowired
	public AddressDAOImpl(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	public void doCreate(Address vo) throws Exception {
		super.getHibernateTemplate().save(vo);

	}

	public void doRemove(Integer id) throws Exception {
		super.getHibernateTemplate().delete(findById(id));
	}

	public void doUpdate(Address vo) throws Exception {
		//super.getHibernateTemplate().update(vo);
		super.getHibernateTemplate().merge(vo);
		
	}

	public List<Address> findAll() throws Exception {
		return super.getHibernateTemplate().loadAll(Address.class);
	}

	public List<Address> findAll(int pageNo, int pageSize, String keyword,
			String column) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Address findById(Integer id) throws Exception {
		return super.getHibernateTemplate().get(Address.class, id);
	}

	public int getAllCount(String keyword, String column) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public Address findByIdAndFlag(int userid, int flag) throws Exception {
		String hql = "FROM Address as a WHERE a.user.userid=? AND a.flag=?";
		Query query = getSession().createQuery(hql);
		query.setLong(0, userid);
		query.setInteger(1, flag);
		return (Address) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Address> findAll(int userid) throws Exception {
		String hql = "FROM Address as a WHERE a.user.userid = ?";
		Query query = getSession().createQuery(hql);
		query.setLong(0, userid);
		return query.list();
	}

}
